package com.springcloud.auth.controller;

import com.springcloud.auth.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @version V1.0
 * @Author 宋聚智
 * @Date 2019/11/4 17:12
 * @description
 * @Modify By：
 * @ModifyTime： 2019/11/4
 * @Modify marker：
 **/
@RestController
@Slf4j
public class AuthController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("login")
    public Map<String,Object> login(String username){
        Map<String,Object> result= new HashMap<>();
        String token = JWTUtil.generateToken(username);
        String refreshToken=JWTUtil.generateRefreshToken();
        log.info("refreshToken：{}",refreshToken);
        redisTemplate.opsForHash().put(refreshToken,"token",token);
        redisTemplate.opsForHash().put(refreshToken,"username",username);
        //设置token 存活时间
        redisTemplate.expire(refreshToken,JWTUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);
        Object token1 = redisTemplate.opsForHash().get(refreshToken, "token");
        log.info("reidsToken，{}",token1.toString());
        result.put("token",token);
        result.put("refreshToken",refreshToken);
        return result;
    }

}
